package com.trt.contentcenter.sentineltest;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 扩展Sentinel - RESTfullURL支持
 */
@Slf4j
@Component
public class MyUrlCleaner implements UrlCleaner {

    @Override
    public String clean(String originUrl) {
        log.info("originUrl = {]", originUrl);
        // 让 /hares/1 与 /hares/2 的返回值相同
        // 返回/shares/{number}

        String[] split = originUrl.split("/");
        return Arrays.stream(split)
              .map(string -> {
                  if (NumberUtils.isNumber(string)) {
                      return "{number}";
                  }
                  return string;
              })
              .reduce((a,b) -> a+"/"+b)
              .orElse("");
    }
}
